package memleak.test.com.testlist;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.RelativeLayout;


import com.gzgd.log.LogUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * Created by runrun on 2017/2/16.
 */

public class ChannelRecyclerViewAdapter extends RecyclerView.Adapter<ChannelRecyclerViewAdapter.ChannelViewHolder> {

    private static final String TAG = "ChannelRecyclerView";

    private List<LiveChannel> mLiveChannels = new ArrayList<>();
    private int mCurrentPosition;
    private OnRecyclerViewItemSelectedListener mOnRecyclerViewItemSelectedListener = new OnRecyclerViewItemSelectedListener() {
        @Override
        public void onItemSelected(int position) {
            LogUtils.d(TAG, "ItemSelect:" + position);
            mCurrentPosition = position;
        }
    };

    public ChannelRecyclerViewAdapter() {
    }

    //根据逻辑频道号获取节目信息
    public static LiveChannel getChannelBeanByLogicNum(List<LiveChannel> list, int logicNum) {
        if (list == null || list.isEmpty()) return null;
        int size = list.size();
        for (int i = 0; i < size; i++) {
            LiveChannel channel = list.get(i);
            if (channel == null) continue;
            if (Integer.parseInt(channel.getcNo()) == logicNum) {
                return channel;
            }
        }
        return null;
    }

    public synchronized void setDatas(List<LiveChannel> list) {
        mLiveChannels.clear();
        LogUtils.d(TAG, "live channel size:" + list.size()); //这里要处理添加999频道到第一个的处理
        mLiveChannels.addAll(list);
    }

    public synchronized void setSingleData(LiveChannel channel) {

        if (mLiveChannels == null) {
            mLiveChannels = new ArrayList<>();
            mLiveChannels.add(channel);
        } else {
            boolean ret = false;
            for (int i = 0; i < mLiveChannels.size(); i++) {
                if (mLiveChannels.get(i).getVideoId().equals(channel.getVideoId())) {
                    long totalTime = mLiveChannels.get(i).getTotalDuration();
                    mLiveChannels.get(i).setTotalDuration(channel.getTotalDuration() + totalTime);
                    ret = true;
                    break;
                }
            }
            if (!ret) {
                mLiveChannels.add(channel);
            }
        }
        if (mLiveChannels.size() > 50) {
            List<LiveChannel> tmp = new ArrayList<>();
            for (int i = 0; i < 50; i++) {
                tmp.add(mLiveChannels.get(i));
            }
            mLiveChannels = tmp;
        }
        Collections.sort(mLiveChannels);

        for (int i = 0; i < mLiveChannels.size(); i++) {
            Log.i(TAG, "CH_NAME:" + mLiveChannels.get(i) + "/Duration:" + mLiveChannels.get(i).getTotalDuration());
        }

    }

    public int getCurrentPosition() {
        return mCurrentPosition;
    }

    public LiveChannel getChannelBeanByLogicNum(int logicNum) {

        if (mLiveChannels == null || mLiveChannels.isEmpty()) return null;
        int size = mLiveChannels.size();
        for (int i = 0; i < size; i++) {
            LiveChannel channel = mLiveChannels.get(i);
            if (channel == null) continue;
            if (Integer.parseInt(channel.getcNo()) == logicNum) {

                LiveApplication mApp = LiveApplication.getGlobalContext();
                VideoBean curVideoBean = mApp.getCurVideoBean();
                if (channel != null && curVideoBean != null && !channel.getVideoId().equals(curVideoBean.getVideoId())) {
                    mApp.changeChannelPos(i);
                }

                return channel;
            }
        }
        return null;
    }

    @Override
    public ChannelViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ChannelView view = (ChannelView) LayoutInflater.from(parent.getContext()).inflate(R.layout.channel_view_layout, null);
        view.setOnRecyclerViewItemSelectedListener(mOnRecyclerViewItemSelectedListener);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(381, 109);
        view.setLayoutParams(params);
        return new ChannelViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ChannelViewHolder holder, int position) {

        holder.getView().setPosition(position);
        holder.getView().updateView(mLiveChannels.get(position));
    }

    @Override
    public int getItemCount() {
        return mLiveChannels.size();
    }

    public LiveChannel getItem(int index) {
        return mLiveChannels.get(index);
    }

    @Override
    public int getItemViewType(int position) {
        return position > 5 ? position % 5 : position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class ChannelViewHolder extends RecyclerView.ViewHolder {
        private ChannelView view;

        public ChannelViewHolder(ChannelView itemView) {
            super(itemView);
            view = itemView;
        }

        ChannelView getView() {
            return view;
        }
    }
}
